package com.azlogica.control;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import android.content.Context;
import android.os.AsyncTask;

public class Connection extends AsyncTask<Void, Void, Void> {

	String dstAddress;
	int dstPort;
	String frame;
	byte[] frameb;
	private static int UDP_SERVER_PORT = 11000;
	int id;
	ManageFrame mf;

	public Connection(Context context, String addr, int port, byte[] frame, int id) {
		mf = new ManageFrame(context);
		dstAddress = addr;
		dstPort = port;
		this.frameb = frame;
		this.id=id;
	}

	@Override
	protected Void doInBackground(Void... params) {
		DatagramSocket ds = null;
		try {
			ds = new DatagramSocket();
			InetAddress serverAddr = InetAddress.getByName(dstAddress);
			DatagramPacket dp;
			dp = new DatagramPacket(frameb, frameb.length,serverAddr, UDP_SERVER_PORT);
			ds.send(dp);
			mf.updateFrame(id,1);
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (ds != null) {
				ds.close();
			}
		}
		return null;

	}

}
